﻿using System;
using System.Windows;
using Airborne.ScrumPoker.Module.Events;

namespace Airborne.ScrumPoker.Module.Developer.Views
{
    /// <summary>
    /// Interaction logic for LoginView.xaml
    /// </summary>
    public partial class LogOnView : Window, ILogOnView
    {
        public LogOnView()
        {
            InitializeComponent();
            comboServer.Text = "localhost:8087";
        }


        #region Subscribe

        public event EventHandler<GenericEventArgs<string>> Subscribe;

        private void RaiseSubscribe(string name)
        {
            GenericEventArgs<string> args = new GenericEventArgs<string>(name);
            OnSubscribe(args);
        }

        protected virtual void OnSubscribe(GenericEventArgs<string> args)
        {
            EventHandler<GenericEventArgs<string>> handler = this.Subscribe;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion


        #region LogOnCanceled

        public event EventHandler<EventArgs> LogOnCanceled;

        private void RaiseLogOnCanceled()
        {
            EventArgs args = EventArgs.Empty;
            OnLogOnCanceled(args);
        }

        protected virtual void OnLogOnCanceled(EventArgs args)
        {
            EventHandler<EventArgs> handler = this.LogOnCanceled;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        public void SetUserName(string name)
        {
            textBoxLogin.Text = name;
        }


        public void ShowView()
        {
            this.Owner = Application.Current.MainWindow;
            this.ShowDialog();
        }

        private void buttonOkClick(object sender, RoutedEventArgs e)
        {
                string serverLocation = string.Concat(textBoxLogin.Text, "@", comboServer.Text);
                RaiseSubscribe(serverLocation);
        }

        private void buttonCancelClick(object sender, RoutedEventArgs e)
        {
            RaiseLogOnCanceled();
            DialogResult = false;
            Close();
        }

        public void CloseView()
        {
            DialogResult = true;
            Close();
        }
    }
}
